package cn.zjh.order.service.impl;

import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.service.IBaiduAiAuditService;
import cn.zjh.basic.service.impl.BaseServiceImpl;
import cn.zjh.basic.util.LoginContext;
import cn.zjh.order.domain.Product;
import cn.zjh.order.domain.ProductDetail;
import cn.zjh.order.domain.ProductOnlineAuditLog;
import cn.zjh.order.mapper.ProductDetailMapper;
import cn.zjh.order.mapper.ProductMapper;
import cn.zjh.order.mapper.ProductOnlineAuditLogMapper;
import cn.zjh.order.service.IProductService;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.mapper.EmployeeMapper;
import cn.zjh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.beans.Transient;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 添加
     * @param product
     */
    @Transient
    @Override
    public void save(Product product){
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 修改
     * @param product
     */
    @Override
    public void upDate(Product product){
        super.upDate(product);
        ProductDetail detail = product.getProductDetail();
        if (detail != null){
            detail.setProductId(product.getId());
            productDetailMapper.upDate(detail);
        }
    }
    @Transient
    @Override
    public void del(Long id){
        super.del(id);
        productDetailMapper.del(id);
    }


    /**
     * 上架审核
     * @param ids
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            Product product = productMapper.findOne(id);
            //上架之前对图文本和图片做审核
            Boolean textAudit = iBaiduAiAuditService.textAudit(product.getName());

            Boolean imageAudit = iBaiduAiAuditService.imageAudit(product.getResources());
            //审核通过之后,修改状态和资料
            if (textAudit && imageAudit){
                product.setOnsaletime(new Date());
                product.setState(1);
                productMapper.upDate(product);

                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setState(1);
                log.setNote("审核通过！");
                productOnlineAuditLogMapper.save(log);

            }else {
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setState(0);//1审核通过  0待审核  -1驳回
                log.setNote("审核失败，含有违规内容！");
                productOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败，内容有非法数据");
            }

        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {

        Date offsaletime = new Date();
        productMapper.offSale(offsaletime,ids);
    }

}
